package com.dianping.cat.consumer.business.model.transform;
import com.dianping.cat.consumer.business.model.IVisitor;
import com.dianping.cat.consumer.business.model.entity.BusinessItem;
import com.dianping.cat.consumer.business.model.entity.BusinessReport;
import com.dianping.cat.consumer.business.model.entity.Segment;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  15 */     this.m_linker = new DefaultLinker(true);




    this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

  public static BusinessReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static BusinessReport parse(InputStream in) {
/*  28 */     DefaultNativeParser parser = new DefaultNativeParser(in);
    BusinessReport businessReport = new BusinessReport();

    try {
      businessReport.accept(parser);
/*  33 */     } catch (RuntimeException e) {
/*  34 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  37 */         throw e;
      }
    }

/*  41 */     parser.m_linker.finish();
/*  42 */     return businessReport;
  }



  public void visitBusinessItem(BusinessItem businessItem) {
    byte tag;
/*  49 */     while ((tag = readTag()) != -1) {
/*  50 */       visitBusinessItemChildren(businessItem, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitBusinessItemChildren(BusinessItem businessItem, int _field, int _type) {
/*  55 */     switch (_field) {
      case 1:
/*  57 */         businessItem.setId(readString());
        break;
      case 2:
/*  60 */         businessItem.setType(readString());
        break;
      case 33:
/*  63 */         if (_type == 1) {
/*  64 */           Segment segment = new Segment();

/*  66 */           visitSegment(segment);
/*  67 */           this.m_linker.onSegment(businessItem, segment); break;
/*  68 */         }  if (_type == 2) {
/*  69 */           for (int i = readInt(); i > 0; i--) {
/*  70 */             Segment segment = new Segment();

/*  72 */             visitSegment(segment);
/*  73 */             this.m_linker.onSegment(businessItem, segment);
          }
        }
        break;
    }
  }



  public void visitBusinessReport(BusinessReport businessReport) {
    byte tag;
/*  84 */     if ((tag = readTag()) != -4) {
/*  85 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/*  88 */     while ((tag = readTag()) != -1) {
/*  89 */       visitBusinessReportChildren(businessReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitBusinessReportChildren(BusinessReport businessReport, int _field, int _type) {
/*  94 */     switch (_field) {
      case 1:
/*  96 */         businessReport.setDomain(readString());
        break;
      case 2:
/*  99 */         businessReport.setStartTime(readDate());
        break;
      case 3:
/* 102 */         businessReport.setEndTime(readDate());
        break;
      case 33:
/* 105 */         if (_type == 1) {
/* 106 */           BusinessItem businessItem = new BusinessItem();

/* 108 */           visitBusinessItem(businessItem);
/* 109 */           this.m_linker.onBusinessItem(businessReport, businessItem); break;
/* 110 */         }  if (_type == 2) {
/* 111 */           for (int i = readInt(); i > 0; i--) {
/* 112 */             BusinessItem businessItem = new BusinessItem();

/* 114 */             visitBusinessItem(businessItem);
/* 115 */             this.m_linker.onBusinessItem(businessReport, businessItem);
          }
        }
        break;
    }
  }



  public void visitSegment(Segment segment) {
    byte tag;
/* 126 */     while ((tag = readTag()) != -1) {
/* 127 */       visitSegmentChildren(segment, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitSegmentChildren(Segment segment, int _field, int _type) {
/* 132 */     switch (_field) {
      case 1:
/* 134 */         segment.setId(Integer.valueOf(readInt()));
        break;
      case 2:
/* 137 */         segment.setCount(readInt());
        break;
      case 3:
/* 140 */         segment.setSum(readDouble());
        break;
      case 4:
/* 143 */         segment.setAvg(readDouble());
        break;
    }
  }

  private Date readDate() {
    try {
/* 150 */       return new Date(readVarint(64));
/* 151 */     } catch (IOException e) {
/* 152 */       throw new RuntimeException(e);
    }
  }

  private double readDouble() {
    try {
/* 158 */       return Double.longBitsToDouble(readVarint(64));
/* 159 */     } catch (IOException e) {
/* 160 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 166 */       return (int)readVarint(32);
/* 167 */     } catch (IOException e) {
/* 168 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 174 */       return this.m_in.readUTF();
/* 175 */     } catch (IOException e) {
/* 176 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 182 */       return this.m_in.readByte();
/* 183 */     } catch (IOException e) {
/* 184 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 189 */     int shift = 0;
/* 190 */     long result = 0L;

/* 192 */     while (shift < length) {
/* 193 */       byte b = this.m_in.readByte();
/* 194 */       result |= (b & 0x7F) << shift;
/* 195 */       if ((b & 0x80) == 0) {
/* 196 */         return result;
      }
/* 198 */       shift += 7;
    }

/* 201 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/business/model/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */